package vivace.view;

import java.awt.*;
import java.awt.event.*;
import java.util.Observer;
import java.util.Observable;
import java.util.Vector;

import vivace.helper.GUIHelper;
import vivace.helper.GridHelper;
import vivace.helper.ProjectHelper;
import vivace.model.*;
import vivace.controller.*;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;
import javax.swing.*;

public class Overview extends JPanel implements Observer{
	
	private Project model;
	private Controller controller;
	
	//CONTAIN???	//CONTAIN???	//CONTAIN??? - THE VARIABLES FOR THE PAINTCOMPONENT
	int		currEvent;
	int		countNumber;
	int		pixelBig;
	int		lengthQuartInPixel;
	int		lengthOfLineX;
	
	double	pixelSmall;
	double	pixelSmallSpace;
	double	ticksPerCurrQuarter;
	double	ticksCounter;
	int 	ticksPerQuarter;
	int[][] timeSignature;
	
	GridHelper gridHelper;
	
	public Overview(Project model) {
		super(null); // Absolute positioning
		this.model = model;
		this.model.addObserver(this);
		this.controller = new Controller();
		this.setBackground(Color.WHITE);
		this.gridHelper = new GridHelper(this.model,GridHelper.Type.OVERVIEW);
		paintTrackBars();
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		gridHelper.paintGrid(g);
	}
	
	// Creates a track bar for each track in the sequence
	private void paintTrackBars(){
		
		// Clear the panel
		this.removeAll();
		
		// Create the track bars
		Track[] tracks = model.getSequencer().getSequence().getTracks();
		for (int i = 0; i < tracks.length; i++) {

			// Is the track empty?
			Vector<MidiEvent> notes = ProjectHelper.filterShortMessages(tracks[i],ShortMessage.NOTE_ON);
			MidiEvent first;
			if( notes.size() > 0 ){

				// Calculate the x and y position of the track bar
				first = notes.firstElement();
				int xStart = gridHelper.tickToXPosition(first.getTick());
				int xEnd = gridHelper.tickToXPosition(tracks[i].ticks());
				int y = i*GUIHelper.TRACK_HEIGHT;
				
				// Create the track bar
				TrackBar trackBar = new TrackBar(i,this.model.getTrackName(i) );
				trackBar.setWidth( xEnd - xStart );
				trackBar.addMouseListener( new Controller() );
				trackBar.setPosition(xStart,y);
				this.add( trackBar );

			}
		} 
		
		// Update the panel's preferred size
		this.setPreferredSize(new Dimension(gridHelper.getWidth(),gridHelper.getHeight()));
	}
	
	@Override
	public void update(Observable o,Object arg) { 
		
		// Check which type of action that was performed
    	Project.Action action = this.model.getAction();
    	
		// Then perform the desired updates depending on the action
    	switch( action ){
    	
    		case ZOOMLEVEL_CHANGED:
    			paintTrackBars();
    			break;
    
    	}
    	
		//repaint();
	}

	private class Controller extends MouseAdapter{

		@Override
		public void mouseClicked(MouseEvent e) {
			if( e.getClickCount() == 2)
				model.setPerspective("pianoRoll");
		}

		@Override
		public void mousePressed(MouseEvent e) {
			TrackBar trackBar = (TrackBar) e.getSource();
			model.setSelectedTrack(trackBar.getIndex());
		}
			
		
		
	}

}

